package com.xn.myyxcampus.fragment;

import java.io.File;

import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.xn.myyxcampus.MainActivity;
import com.xn.myyxcampus.MyApplication;
import com.xn.myyxcampus.R;
import com.xn.myyxcampus.adapter.MenuAdapter;
import com.xn.myyxcampus.utils.ActivityUtils;
import com.xn.myyxcampus.utils.FileHelper;
import com.xn.myyxcampus.utils.MyPreferences;

public class MenuFragment extends BaseFragment implements OnItemClickListener {

	private ListView lv_menuList;
	private int[] menuImgNormal = { R.drawable.campus_news_normal,
			R.drawable.campus_voice_normal, R.drawable.campus_shop_normal,
			R.drawable.campus_show_normal, R.drawable.campus_setting_normal,
			R.drawable.campus_login_normal, R.drawable.campus_off_login_normal };
	// private int[] menuImgPressed = { R.drawable.campus_news_pressed,
	// R.drawable.campus_voice_pressed, R.drawable.campus_shop_pressed,
	// R.drawable.campus_show_pressed, R.drawable.campus_setting_pressed,
	// R.drawable.campus_login_pressed,
	// R.drawable.campus_off_login_pressed };

	private ImageView leftHead;
	private String[] menuItem = { "校园新闻", "校园叽喳", "校园商家", "物品交换", "设置", "注册",
			"登录" };
	String path = Environment.getExternalStorageDirectory()
			+ "/DCIM/Tracy/HeadPhoto";
	private File leftFile = new File(Environment.getExternalStorageDirectory()
			+ "/DCIM/Tracy/HeadPhoto");

	private MenuAdapter adapter;

	private Bitmap headBitmap;

	@Override
	public View initView(LayoutInflater inflater) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.menu, null);
		lv_menuList = (ListView) view.findViewById(R.id.lv_menuList);
		leftHead = (ImageView) view.findViewById(R.id.iv_leftHead);
		if (MyPreferences.isLoginIn(MyApplication.getContext())) {
			if (leftFile.exists()) {
				File[] files = leftFile.listFiles();
				if (files.length != 0) {
					headBitmap = BitmapFactory
							.decodeFile(files[files.length - 1]
									.getAbsolutePath());
					for (int index = 0; index < files.length - 1; index++) {
						files[index].deleteOnExit();
					}
					leftHead.setImageBitmap(headBitmap);
				}
			}
		}
		return view;
	}

	@Override
	public void initData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (MyPreferences.isLoginIn(MyApplication.getContext())) {
			menuItem[menuItem.length - 1] = "注销";
		}
		adapter = new MenuAdapter(MyApplication.getContext(), menuImgNormal,
				menuItem);
		lv_menuList.setAdapter(adapter);
		lv_menuList.setOnItemClickListener(MenuFragment.this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Fragment f = null;
		for (int i = 0; i < menuItem.length; i++) {
			if (position == i) {
				parent.getChildAt(i).setBackgroundResource(
						R.drawable.text_pressed);
			} else {
				parent.getChildAt(i).setBackgroundResource(
						R.drawable.left_text_normal);
			}
		}
		adapter.notifyDataSetChanged();
		switch (position) {
		case 0:
			f = new NewsFragment();
			break;
		case 1:
			f = new VoiceFragment();
			break;
		case 2:
			f = new ShopFragment();
			break;
		case 3:
			f = new ExchangeFragment();
			break;
		case 4:
			f = new SettingFragment();
			break;
		case 5:
			f = new RegisterFragment();
			break;
		case 6:
			if("登录".equals(menuItem[menuItem.length-1])) {
				f = new LoginFragment();
			} else {
				FileHelper.clearFiles(path);
				Editor edit = MyPreferences.getShare().edit();
				edit.putString("name", "");
				edit.putString("pass", "");
				edit.putString(MyPreferences.TOKEN, "");
				edit.commit();
				ActivityUtils.finishAllActivity();
			}
			break;
		default:
			break;
		}
		switchFragment(f);
	}

	private void switchFragment(Fragment f) {
		if (null != f) {
			if (getActivity() instanceof MainActivity) {
				((MainActivity) getActivity()).switchFragment(f);
			}
		}
	}
}
